// pages/project/index.js
var api = require('../../config/api.js');
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 0,
    tabs: [],
    /** 页面list初始化数据 */
    currentPage: 1, //请求业索引
    pageSize: 10, //请求业数据大小
    pages: 0, //总页数
    totalSize: 0, //条目总数
    projectList: [],
    type: 'projectPlan',
    timeType: '预计申报时间'
  },
  change(e){
    var index = e.currentTarget.dataset.index;
    var timeType;
    var type;
    if(index == 0){
      timeType = '预计申报时间';
      type = 'projectPlan';
    }else if(index == 1){
      timeType = '截稿时间';
      type = 'projectWrite';
    }else if(index == 2){
      timeType = '下一阶段预计时间';
      type = 'projectMonitor';
    }else if(index == 3){
      timeType = '维护时间';
      type = 'projectMaintain';
    }
    this.setData({
      current: index,
      timeType: timeType,
      type: type,
      currentPage: 1
    })
    this.getProjectList();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.getProjectPageData();
    this.getProjectList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var currentPage = that.data.currentPage;
    console.log(currentPage);
    that.setData({
      currentPage: currentPage + 1
    });
    that.getProjectList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /** 页面加载获取数据 */
  getProjectPageData: function () {
    var that = this;
    util.request(api.getProjectPageData, {
      userId: wx.getStorageSync('appUserInfo').id,
      type: 'projectPlan'
    }, 'GET').then(res => {
      if (res.result == 1) {
        that.setData({
          tabs: [
            {
              name: '项目规划',
              amount: res.info.projectPageData.projectPlanNum
            },
            {
              name: '项目撰写',
              amount: res.info.projectPageData.projectWriteNum
            },
            {
              name: '项目监控',
              amount: res.info.projectPageData.projectMonitorNum
            },
            {
              name: '项目维护',
              amount: res.info.projectPageData.projectMaintainNum
            },
          ],
        });
      }
    });
  },

  //获取项目列表list
  getProjectList: function () {
    var that = this;
    util.request(api.getProjectList, {
      currentPage: that.data.currentPage,
      pageSize: that.data.pageSize,
      userId: wx.getStorageSync('appUserInfo').id,
      type: that.data.type
    }, 'GET').then(res => {
      if (res.result == 1) {
        var list = res.info.list;
        var tempList = that.data.projectList;
        var tempCurrentPage = that.data.currentPage;
        if (that.data.currentPage == 1) {
          tempList = list;
          tempCurrentPage = 1;
          wx.stopPullDownRefresh();
        } else {
          tempList = tempList.concat(list);
        }
        that.setData({
          currentPage: tempCurrentPage,
          pageSize: res.info.pageSize,
          pages: res.info.pages,
          totalSize: res.info.totalSize,
          projectList: tempList
        });
      }
    });
  },

})